package com.binance.healthcenter.notification;

import com.binance.healthcenter.model.HealthCheckResult;
import com.binance.healthcenter.telegram.service.TelegramBotService;
import com.binance.master.utils.JsonUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;

import java.util.List;

@Log4j2
@Service
public class TelegramNotificationService {

    @Autowired
    private TelegramBotService telegramBotService;
    @Value("${com.binancen.telegram.bot.notification.chatId}")
    private String configChatId;


    public void sendTextMessage(List<HealthCheckResult> healthCheckResultList) {
       for(HealthCheckResult healthCheckResult:healthCheckResultList){
           sendTextMessage(JsonUtils.toJsonNotNullKey(healthCheckResult));
       }
    }


    public void sendTextMessage(String text) {
        try {
            log.info("TelegramNotificationService.sendTextMessage message={}", text);
            SendMessage message = new SendMessage();
            message.setChatId(configChatId);
            message.setText(text);
            telegramBotService.execute(message);
        } catch (Exception e) {
            log.error("TelegramNotificationService.sendTextMessage exception:", e);
        }
    }

    public void sendTextMessage(String chatId, String text) {
        try {
            log.info("TelegramNotificationService.sendTextMessage :chatId={},text={}", chatId, text);
            SendMessage message = new SendMessage();
            message.setChatId(chatId);
            message.setText(text);
            telegramBotService.execute(message);
        } catch (Exception e) {
            log.error("TelegramNotificationService.sendTextMessage with chatId exception:", e);
        }
    }


}
